//
//  VideoCollectViewController.m
//  LOL_JewelBox
//
//  Created by dllo on 15/5/2.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "VideoCollectViewController.h"
#import "NewsCell.h"
#import "NewsView.h"
#import "CollectModel.h"
#import "DetailCollectViewController.h"
@interface VideoCollectViewController ()
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *VideoArr;
@property(nonatomic, retain)UIImageView *pictureImage;
@property(nonatomic, copy)NSString *documentStr;
@end

@implementation VideoCollectViewController

- (void)dealloc
{
    [_tableView release];
    [_VideoArr release];
    [_pictureImage release];
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.VideoArr = [NSMutableArray array];

    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NewsView *newsView = [[NewsView alloc]initWithFrame:CGRectMake(0, 0, VIEWWIDTH, VIEWHEIGHT)];
    [self.view addSubview:newsView];
    newsView.tag = 1000;
    [newsView release];
    
    self.pictureImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, VIEWWIDTH, VIEWHEIGHT )];
    self.pictureImage.image = [UIImage imageNamed:@"EZ"];
    [newsView addSubview:self.pictureImage];
    [_pictureImage release];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, VIEWWIDTH, VIEWHEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = NO;
    self.tableView.rowHeight = 60;
    self.tableView.backgroundView = self.pictureImage;
    [_tableView release];
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(Click:)];
    [self GetVideo];
}

#pragma mark 控制按钮
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}
#pragma mark 添加删除置顶

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *addAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            //更新数据
        [self.VideoArr exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
        //归档
        [NSKeyedArchiver archiveRootObject:self.VideoArr toFile:self.documentStr];
        //更新UI
        NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
        [self.tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
    }];
    addAction.backgroundColor = [UIColor redColor];
    
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            //更新数据
        [self.VideoArr removeObjectAtIndex:indexPath.row];
        //归档
        [NSKeyedArchiver archiveRootObject:self.VideoArr toFile:self.documentStr];
        //更新数据
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }];
    deleteAction.backgroundColor = [UIColor orangeColor];
    return @[addAction,deleteAction];
}

#pragma mark 移动

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    
    CollectModel *collectMo = [self.VideoArr[sourceIndexPath.row]retain];
    [self.VideoArr removeObjectAtIndex:sourceIndexPath.row];
    [self.VideoArr insertObject:collectMo atIndex:destinationIndexPath.row];
    //归档
    [NSKeyedArchiver archiveRootObject:self.VideoArr toFile:self.documentStr];
    [collectMo release];
}

#pragma mark 视频反归档

- (void)GetVideo{
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sandBoxPath = [sandBox lastObject];
    NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"视频收藏"];
    self.documentStr = [documentPath stringByAppendingPathComponent:@"videoCollect.plist"];
    self.VideoArr = [NSKeyedUnarchiver unarchiveObjectWithFile:self.documentStr];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.VideoArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse]autorelease];
    }
    CollectModel *collectMo = self.VideoArr[indexPath.row];
    cell.textLabel.text = collectMo.title;
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.font = [UIFont systemFontOfSize:18];
#pragma mark 点击Cell不变色
    cell.selectionStyle = NO;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailCollectViewController *detailView = [[DetailCollectViewController alloc]init];
    [self.navigationController pushViewController:detailView animated:YES];
    CollectModel *collectMo = self.VideoArr[indexPath.row];
    detailView.url = collectMo.url;
    [detailView release];
    
}

-(void)Click:(id)sander
{
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
